<?php

namespace App\Http\Controllers\Home;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
// use Illuminate\Http\Response;
use App\Model\District;
use DB;


/**
 * @author 张健领
 * 详情页ajax处理
 */
class ShopController extends Controller
{
    //城市三级联动
    public function Area()
    {
        
        $upid = intval($_POST['upid']);
        $data = District::select('id','name','upid')->where('upid', $upid)->get();
        if ($data) {
            $result = [
                'code' => 200,
                'msg'  => '成功',
                'data' => $data,
            ];
        }
        
        return json_encode($result);
    }

    //商品属性读取
    public function attrs(Request $request)
    {
        // return 1;
        $tp = $request->input('tp');
        $tid = $request->input('gid');
        $val = $request->input('val');

        // dd($request->input());
        if ($tp == 'memory') {
            $data = DB::table('shop_price')->select('price')->where([['gid' , $tid],[$tp, $val] ])->first();
        }
         if ($tp == 'color') {
            $data = DB::table('shop_price')->select('store')->where([['gid' , $tid],[$tp, $val] ])->first();
        }
        // dd($data);  

        return json_encode($data);

    }
    
    public function attrSearch(Request $request)
    {
        $gid = $request->input('gid');
        $color = $request->input('color');
        $memory = $request->input('memory');
        $data = DB::table('shop_price')->select('id')->where([['gid' , $gid],['color', $color] , ['memory', $memory]])->first();
        if (!$data) {
            return false;
        }
        return json_encode($data);
    }


    public function TypeSearch($typeid)
    {
        if ($typeid) {
             $shop = DB::table('shop_goods')
                            ->join('shop_price', 'shop_goods.id', '=', 'shop_price.gid')
                            ->select('shop_goods.id','shop_goods.gname','shop_goods.goods_sales','shop_goods.clicknum','shop_goods.pic', 'shop_price.price','shop_price.memory','shop_price.color')
                            ->where('typeid',$typeid)
                            ->paginate(8);
           
        } else {
             $shop = DB::table('shop_goods')
                            ->join('shop_price', 'shop_goods.id', '=', 'shop_price.gid')
                            ->select('shop_goods.id','shop_goods.gname','shop_goods.goods_sales','shop_goods.clicknum','shop_goods.pic', 'shop_price.price','shop_price.memory','shop_price.color')
                            ->paginate(8);

            
        }
        // dd($shop);
        // 
        $pin = DB::table('shop_type')->select('id','name','pid')->where('pid',0)->get();
        // dd($pin);

            return view('Home/TypeList'  , [ 
                                                'shop' => $shop,
                                                'pin' =>$pin
                                            ]);
    }


    public function NextId(Request $request)
    {
        $tid = $request->input('tid');
        // dd($tid);
        $type = DB::table('shop_type')->select('id','name','pid')->where('pid',$tid)->get();
        // dd($type);
        if (!$type) {
            return false;
        }
        return json_encode($type);



    }

}
